package org.rslai.tcedit.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

class UserSessionInfo implements Serializable {
	private static final long serialVersionUID = 1094752230301223817L;

	private String productName = "";
	private String productID = "";
	private String versionName = "";
	private String versionID = "";

	public void setProductName(String productName) {
		this.productName = productName;
	}
	public String getProductName() {
		return this.productName;
	}

	public void setProductID(String productID) {
		this.productID = productID;
	}
	public String getProductID() {
		return this.productID;
	}

	public void setVersionName(String versionName) {
		this.versionName = versionName;
	}
	public String getVersionName() {
		return this.versionName;
	}

	public void setVersionID(String versionID) {
		this.versionID = versionID;
	}
	public String getVersionID() {
		return this.versionID;
	}
}

public class SessionUtil {

	/**
	 * 返回当前用户选择的产品名称
	 * @param request request
	 * @return
	 */
	public static String getCurrentProductName(HttpServletRequest request) {
		return getUserSessionInfo(request).getProductName();
	}

	/**
	 * 设置当前用户选择的产品名称
	 * @param request
	 * @param productName
	 */
	public static void setCurrentProductName(HttpServletRequest request, String productName) {
		getUserSessionInfo(request).setProductName(productName);
	}

	/**
	 * 返回当前用户选择的产品ID
	 * @param request request
	 * @return
	 */
	public static String getCurrentProductID(HttpServletRequest request) {
		return getUserSessionInfo(request).getProductID();
	}

	/**
	 * 设置当前用户选择的产品ID
	 * @param request
	 * @param productID
	 */
	public static void setCurrentProductID(HttpServletRequest request, String productID) {
		getUserSessionInfo(request).setProductID(productID);
	}

	/**
	 * 返回当前用户选择的版本名称
	 * @param request request
	 * @return
	 */
	public static String getCurrentVersionName(HttpServletRequest request) {
		return getUserSessionInfo(request).getVersionName();
	}

	/**
	 * 设置当前用户选择的版本名称
	 * @param request
	 * @param versionName
	 */
	public static void setCurrentVersionName(HttpServletRequest request, String versionName) {
		getUserSessionInfo(request).setVersionName(versionName);
	}

	/**
	 * 返回当前用户选择的版本ID
	 * @param request request
	 * @return
	 */
	public static String getCurrentVersionID(HttpServletRequest request) {
		return getUserSessionInfo(request).getVersionID();
	}

	/**
	 * 设置当前用户选择的版本ID
	 * @param request
	 * @param versionID
	 */
	public static void setCurrentVersionID(HttpServletRequest request, String versionID) {
		getUserSessionInfo(request).setVersionID(versionID);
	}

	/**
	 * 从 Session 中取得用户保存的信息
	 * @param request
	 * @return
	 */
	private static UserSessionInfo getUserSessionInfo(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute("userSessionInfo");
		if ( obj == null ) {
			UserSessionInfo userSessionInfo = new UserSessionInfo();
			setUserSessionInfo(request, userSessionInfo);
			return userSessionInfo;
		} else {
			return (UserSessionInfo) obj;
		}
	}

	/**
	 * 保存用户信息到 Session 中
	 * @param request
	 * @param userSessionInfo
	 */
	private static void setUserSessionInfo(HttpServletRequest request, UserSessionInfo userSessionInfo) {
		request.getSession().setAttribute("userSessionInfo", userSessionInfo);
	}

	/**
	 * 从 Session 中取得保存的参数信息
	 * @param request
	 * @return 参数map
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> getParametersSessionInfo(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute("parametersSessionInfo");
		if ( obj == null ) {
			Map<String, String> parametersMap = new HashMap<String, String>();
			setParametersSessionInfo(request, parametersMap);
			return parametersMap;
		} else {
			return (Map<String, String>)obj;
		}
	}

	/**
	 * 保存参数信息到 Session 中
	 * @param request
	 * @param parametersMap
	 */
	public static void setParametersSessionInfo(HttpServletRequest request, Map<String, String> parametersMap) {
		request.getSession().setAttribute("parametersSessionInfo", parametersMap);
	}

}
